﻿using DataAcquisition;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace gl_SMT
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            Button closeButton = sender as Button;
            if (closeButton == null) return;

            // 找到所属的TabItem
            TabItem tabItem = FindParent<TabItem>(closeButton);
            if (tabItem == null) return;
            if (tabItem.Header.ToString() == "GKG自动印刷机") VariableStorage.gkg_Running = false;
            if (tabItem.Header.ToString() == "回流焊接机") VariableStorage.hl_Running = false;
            if (tabItem.Header.ToString() == "雅马哈贴片机") VariableStorage.ymh_Running = false;
            if (tabItem.Header.ToString() == "JUKI贴片机") VariableStorage.juki_Running = false;
            // 从TabControl中移除
            TabControl tabControl = ItemsControl.ItemsControlFromItemContainer(tabItem) as TabControl;
            if (tabControl != null)
            {
                if (tabItem.Header.ToString() == "机台类型选择") return;
                    tabControl.Items.Remove(tabItem);
            }

            e.Handled = true;
        }

        private T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);
            if (parentObject == null) return null;

            T parent = parentObject as T;
            return parent ?? FindParent<T>(parentObject);
        }
    }

}
